package com.ymatou.iapi.godfreightmgr.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.DeleteTemplateInfoBean;
import com.ymatou.iapi.godfreightmgr.service.DeleteTemplateInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.database.model.freight_template;
import com.ymttest.database.sqlwapper.godfreight.freight_templateWapper;
import com.ymttest.utils.AgentUsers;
/**
*删除运费模板
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteTemplateInfo{
	private static DeleteTemplateInfoBean deletetemplateinfoBean;
	private static DeleteTemplateInfoCall deletetemplateinfoCall;
	private static int sellerId = AgentUsers.rootUserId;
	private static freight_templateWapper templateWapper = new freight_templateWapper();
	private static String sellerName = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除运费模板");
	}

	@Before
	public void caseUp() {
		deletetemplateinfoBean=new DeleteTemplateInfoBean();
		deletetemplateinfoCall=new DeleteTemplateInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deletetemplateinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteTemplateInfo_01() {
		Logger.start(true,"删除运费模板，删除成功");
		try {
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(sellerId, 1, 1);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			deletetemplateinfoBean.setSellerId(sellerId);
			deletetemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("200",deletetemplateinfoCall.getString("code"),"验证code");
			freight_template template = templateWapper.selectByPrimaryKey(Integer.parseInt(templateId));
			Logger.verifyEquals(-1,template.getTemplateStatus(),"验证，验证模板已经删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteTemplateInfo_02() {
		Logger.start(true,"删除运费模板，模板id不存在，删除成功");
		try {
			deletetemplateinfoBean.setSellerId(sellerId);
			deletetemplateinfoBean.setTemplateId(0);
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("400",deletetemplateinfoCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteTemplateInfo_03() {
		Logger.start(true,"删除运费模板，用户id不存在，删除成功");
		try {
			deletetemplateinfoBean.setSellerId(0);
			deletetemplateinfoBean.setTemplateId(0);
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("400",deletetemplateinfoCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteTemplateInfo_04() {
		Logger.start(true,"删除运费模板，已有商品正在使用该模板，删除失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,false);
			addAgentProductBean.getCatalogList().forEach(x->x.setWeight(5.00));
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(sellerId, 2, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			addAgentProductBean.setFreightTemplateId(Integer.parseInt(templateId));

			AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			deletetemplateinfoBean.setSellerId(sellerId);
			deletetemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("400",deletetemplateinfoCall.getString("code"),"验证code");
			//删除运费模板
			templateWapper.updateStatusByKey(Integer.parseInt(templateId));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteTemplateInfo_05() {
		Logger.start(true,"删除运费模板，已有商品正在使用该模板，删除失败，商品更换模板，再次删除模板，删除成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,false);
			addAgentProductBean.getCatalogList().forEach(x->x.setWeight(5.00));
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(sellerId, 2, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);

			CreateFreightTemplateBean createFreightTemplateBean1 = GodfreightmgrService.initCreateFreightTemplate(sellerId, 2, 2);
			String templateId1 = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean1);

			addAgentProductBean.setFreightTemplateId(Integer.parseInt(templateId));

			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			deletetemplateinfoBean.setSellerId(sellerId);
			deletetemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("400",deletetemplateinfoCall.getString("code"),"验证code");

			ModifyAgentProductBean modifyAgentProductBean = AddAgentProductCallService.initModifyAgentProductBean(productId, addAgentProductBean);
			modifyAgentProductBean.setFreightTemplateId(Integer.parseInt(templateId1));
			//删除运费模板
			AddAgentProductCallService.modifyAgentProduct(modifyAgentProductBean);

			deletetemplateinfoBean.setSellerId(sellerId);
			deletetemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			deletetemplateinfoCall.setData(deletetemplateinfoBean);
			deletetemplateinfoCall.callService();
			Logger.verifyEquals("200",deletetemplateinfoCall.getString("code"),"验证code");

			templateWapper.updateStatusByKey(Integer.parseInt(templateId1));
			templateWapper.updateStatusByKey(Integer.parseInt(templateId));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}